package com.samapp.indent;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.samapp.MyApplication;
import com.samapp.R;
import com.samapp.base.BaseActivity;
import com.samapp.entity.TestMethod;
import com.samapp.utils.json.GsonUtil;

public class ViewTestMethodActivity extends BaseActivity implements OnClickListener {

	private Button btnBack = null;
	private EditText cname  = null;
	private EditText ename = null;
	private EditText price = null;
	private EditText period = null;
	private EditText isCnas = null;
	private EditText isCma = null;
	private EditText isCmaf = null;
	private EditText isCatl = null;
	
	private MyApplication myApplication = MyApplication.getApplicationInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_test_method);
		init();
	}

	private void init() {
		initView();
		initListener();
		initData();
	}

	private void initView() {
		btnBack = (Button) findViewById(R.id.btn_back);
		cname = (EditText) findViewById(R.id.cname);
		ename = (EditText) findViewById(R.id.ename);
		price = (EditText) findViewById(R.id.price);
		period = (EditText) findViewById(R.id.period);
		isCnas = (EditText) findViewById(R.id.isCnas);
		isCma = (EditText) findViewById(R.id.isCma);
		isCmaf = (EditText) findViewById(R.id.isCmaf);
		isCatl = (EditText) findViewById(R.id.isCatl);
		
	}
	
	private void initListener() {
		btnBack.setOnClickListener(this);
	}
	
	private void initData(){
		String url = myApplication.url + "queryTestMethod.htm";
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("accessToken", "5C9.yx0S");
		params.addQueryStringParameter("FId", myApplication.curDetetionFid);
		new HttpUtils().send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				Log.i("Http", responseInfo.result);
				try {
					TestMethod testMethod = GsonUtil.getGson().fromJson(responseInfo.result,TestMethod.class);
					
					cname.setText(testMethod.getCname());
					ename.setText(testMethod.getEname());
					price.setText(testMethod.getPrice()+"");
					period.setText(testMethod.getPeriod());
					if(testMethod.getIsCnas().equals("true")){
						isCnas.setText("是");
					}else{
						isCnas.setText("否");
					}
					if(testMethod.getIsCma().equals("true")){
						isCma.setText("是");
					}else{
						isCma.setText("否");
					}
					if(testMethod.getIsCmaf().equals("true")){
						isCmaf.setText("是");
					}else{
						isCmaf.setText("否");
					}
					if(testMethod.getIsCatl().equals("true")){
						isCatl.setText("是");
					}else{
						isCatl.setText("否");
					}
					
					dismissLoadingDialog();
				} catch (Exception e) {
					e.printStackTrace();
					dismissLoadingDialog();
				}
			}

			@Override
			public void onFailure(HttpException e, String arg1) {
				e.printStackTrace();
				dismissLoadingDialog();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			skip(DetectionItemListActivity.class);
			finish();
			break;
		}
	}
	

	/** 跳转页面 */
	private void skip(Class<?> cls) {
		Intent intent = new Intent(this, cls);
		startActivity(intent);
	}
	
	/** 修改返回键 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		btnBack.performClick();
	}

}
